package com.lanmei.group.weixin.common;



import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.lanmei.group.weixin.controller.IndexController;
import com.lanmei.group.weixin.controller.admin.AdminController;
import com.lanmei.group.weixin.controller.admin.CmsArticleController;
import com.lanmei.group.weixin.controller.admin.CmsCategoryControlloer;
import com.lanmei.group.weixin.controller.admin.ManagerInfoController;
import com.lanmei.group.weixin.controller.admin.RoleInfoController;
import com.lanmei.group.weixin.controller.admin.SiteInfoController;
import com.lanmei.group.weixin.controller.admin.UploadFileController;
import com.lanmei.group.weixin.interceptor.ManagerInterceptor;
import com.lanmei.group.weixin.interceptor.SiteInterceptor;
import com.lanmei.group.weixin.model._MappingKit;

public class CommonConfig extends JFinalConfig{
	
	private Routes routes;

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setViewType(ViewType.JSP);
		PropKit.use("config.properties");
		/*me.setBaseUploadPath(PathKit.getWebRootPath()+"/../upload");*/
		me.setBaseUploadPath(PropKit.get("uploadPath"));
		me.setJsonFactory(new FastJsonFactory());
		//RequiresGuest，RequiresAuthentication，RequiresUser验证异常，返回HTTP401状态码
		/*me.setErrorView(401, "views/admin/login.jsp");
		//RequiresRoles，RequiresPermissions授权异常,返回HTTP403状态码
		me.setErrorView(403, "/views/admin/login.jsp");*/
	}

	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		me.add("/admin", AdminController.class,"views/admin");
		me.add("/admin/site", SiteInfoController.class,"views/admin/site");
		me.add("/admin/managerinfo", ManagerInfoController.class,"views/admin/managerinfo");
		me.add("/admin/roleinfo", RoleInfoController.class,"views/admin/roleinfo");
		me.add("/admin/cmscategory", CmsCategoryControlloer.class,"views/admin/cmscategory");
		me.add("/admin/cmsarticle", CmsArticleController.class,"views/admin/cmsarticle");
		me.add("/admin/uploadfile", UploadFileController.class,"views/admin/uploadfile");
	}

	@Override
	public void configPlugin(Plugins me) {
		C3p0Plugin cp = new C3p0Plugin(PropKit.use("config.properties").get("jdbcUrl"),PropKit.use("config.properties").get("username"), PropKit.use("config.properties").get("password"));
		me.add(cp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(cp);
		me.add(arp);
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		arp.setDialect(new MysqlDialect());
		_MappingKit.mapping(arp);
		
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
	    shiroPlugin.setLoginUrl("/admin/index");//登陆url：未验证成功跳转
	    shiroPlugin.setSuccessUrl("/admin/toIndex");//登陆成功url：验证成功自动跳转
	    shiroPlugin.setUnauthorizedUrl("/admin/index");//授权url：未授权成功自动跳转
	    me.add(shiroPlugin);
		

	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new ShiroInterceptor());
		me.addGlobalActionInterceptor(new SiteInterceptor());
		me.addGlobalActionInterceptor(new ManagerInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {

	}
	public static void main(String[] args) {
		//JFinal.start("src/main/webapp", 80, "/", 5);
	}
}
